﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;
using EmailCenter_SEK53.Models.Objects;

namespace EmailCenter_SEK53.Models
{
    public class Repository : IRepository
    {
        private string connectionString;
        private EmailCenterDataClassesDataContext _db;

        // Constructor: initial database context
        public Repository()
        {
            string inipath = Application.StartupPath + "\\config.ini";
            IniFile inifile = new IniFile(inipath);
            connectionString = inifile.IniReadValue("Database", "ConnectionString");
            _db = new EmailCenterDataClassesDataContext(connectionString);
        }

        #region Implement interface
        // Method get email list by type: 1 - inbox, 2- Sent Mail, 3- Draft
        public IList<Email> GetEmailList(int type, int accountId)
        {
            var emailList = (from e in _db.Emails
                             where e.type == type && e.accountId == accountId
                             select e).ToList();
            return emailList;
        }

        // Method insert new email
        public void InsertNewEmail(int type, string partnerEmail, string partnerName, string subject, string datetime, string contents, int accountId)
        {
            var newEmail = new Email();
            newEmail.type = type;
            newEmail.partnerEmail = partnerEmail;
            newEmail.partnerName = partnerName;
            newEmail.subject = subject;
            newEmail.dateTime = datetime;
            newEmail.contents = contents;
            newEmail.accountId = accountId;

            _db.Emails.InsertOnSubmit(newEmail);
            _db.SubmitChanges();
        }

        // Method update an email by email ID
        public void UpdateEmail(int emailId, int type, string partnerEmail, string partnerName, string subject, string datetime, string contents, int accountId)
        {
            var emailToUpdate = (from e in _db.Emails
                                 where e.emailId == emailId
                                 select e).ToList().First();
            emailToUpdate.type = type;
            emailToUpdate.partnerEmail = partnerEmail;
            emailToUpdate.partnerName = partnerName;
            emailToUpdate.subject = subject;
            emailToUpdate.dateTime = datetime;
            if (contents.Length > 4000)
                emailToUpdate.contents = contents.Substring(0, 3999);
            else
                emailToUpdate.contents = contents;
            emailToUpdate.accountId = accountId;

            _db.SubmitChanges();
        }

        // Method delete an email by email ID
        public void DeleteEmail(int emailId)
        {
            var emailToDelete = (from e in _db.Emails
                                 where e.emailId == emailId
                                 select e).ToList();
            foreach (Email e in emailToDelete)
            {
                _db.Emails.DeleteOnSubmit(e);
                _db.SubmitChanges();
            }
        }

        // Method check acount is already exist
        public bool isAlreadyExistAccount(string email)
        {
            var emailList = (from ac in _db.Accounts
                             where ac.email == email
                             select ac).ToList();
            if (emailList.Count > 0)
                return true;
            return false;
        }

        public int isAccountExist(string email)
        {
            var acc = (from ac in _db.Accounts
                             where ac.email == email
                             select ac).ToList();
            if (acc.Count == 0)
                return -1;
            else
            {
                Account a = acc.First();
                return a.accountId;
            }
        }

        // Method insert new account
        public int InsertNewAccount(string email, string userName, string password)
        {
            var newAccount = new Account();
            
            newAccount.email = email;
            newAccount.userName = userName;
            newAccount.password = password;

            _db.Accounts.InsertOnSubmit(newAccount);
            _db.SubmitChanges();

            var lastAcc = (from a in _db.Accounts
                           select a).ToList().Last();

            return lastAcc.accountId;
        }

        // Method get suggest email
        public IList<SuggestEmailItem> GetSuggestEmail()
        {
            SuggestEmailItem sei;
            List<SuggestEmailItem> suggestEmailList = new List<SuggestEmailItem>();

            var emailList = (from e in _db.Emails
                             select e).ToList();
            var accountList = (from a in _db.Accounts
                               select a).ToList();
            
            foreach (var e in emailList)
            {
                sei = new SuggestEmailItem(e.partnerEmail, e.partnerName);
                suggestEmailList.Add(sei);
            }
            foreach (var a in accountList)
            {
                sei = new SuggestEmailItem(a.email, a.userName);
                suggestEmailList.Add(sei);
            }
            return suggestEmailList;
        }
        #endregion
    }
}
